<template>
  <div class="device">
    <el-row :gutter="10">
      <el-col :span="24">
        <el-card class="box-card mt-15">
          <div class="home-form">
            <el-form ref="ruleForm" label-width="60px" @submit.native.prevent>
              <el-row :gutter="10">
                <el-col :span="4">
                  <el-form-item label="店铺" prop="shop">
                    <el-select v-model="shop" placeholder="请选择" style="width:100%" size="mini" clearable>
                      <el-option
                        v-for="item in shopArr"
                        :key="item.id"
                        :label="item.storeName"
                        :value="item.id">
                      </el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="4">
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch" class="search-btn">搜索</el-button>
                </el-col>
              </el-row>
            </el-form>
          </div>
        </el-card>
        <el-card class="box-card">
          <!-- 头部  -->
          <div slot="header" class="clearfix fix-lh">
            <span>设备管理</span>
            <div class="btn-list">
              <el-button size="mini" @click="handleAdd" v-hasPermission="permission.add">新增</el-button>
            </div>
          </div>
          <div class="home-body">
            <cvue-table
              :option="tableOption"
              :data="tableData"
              ref="tbl1"
              :isShowAdd="false"
              :page="page"
              :tablePage="tablePage"
              :pageSize="page.pageSize"
              :loading="tableLoading"
              @current-change="handleCurrentChange"
              @size-change="handleSizeChange"
              @select-change="handleSelectChange"
              :isShowPage="true">
              <template slot-scope="scope" slot="onlineFlag">
                <span>{{ scope.row.onlineFlag == 1 ? '在线' : '离线' }}</span>
              </template>
              <template slot-scope="scope" slot="region">
                <span>{{ scope.row.region == 1 ? '私教区' : '自由区' }}</span>
              </template>
              <!-- 操作按钮 -->
              <el-table-column fixed="right" slot="actionMenu" label="操作" align="center" header-align="center" width="160">
                <template slot-scope="scope">
                  <el-button type="text" size="small" @click="handleEdit(scope.row)">编辑</el-button>
                  <!-- <el-button type="text" size="small" @click="handleView(scope.row)">查看</el-button> -->
                  <!-- <el-button type="text" size="small">闸机流水</el-button> -->
                </template>
              </el-table-column>
            </cvue-table>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {mapGetters} from 'vuex'
import cvueTable from '@/components/cvue-table'
export default {
  name: 'device',
  components: {
    cvueTable
  },
  data () {
    return {
      tablePage: 1,
      tableLoading: false,
      page: {
        total: 1, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
        pageSizes: [10, 50, 100]
      },
      tableOption: {
        stripe: true,
        width: '100%',
        border: true,
        index: false,
        selected: false,
        cloumn: [
          { prop: 'deviceName', label: '设备名称', align: 'center', sortable: true },
          { prop: 'ip', label: 'IP地址', align: 'center', sortable: true },
          { prop: 'port', label: '端口', align: 'center', sortable: true },
          { prop: 'deviceControllerAppCode', label: '归属门店', align: 'center', sortable: true },
          { prop: 'region', label: '所在区域', align: 'center', sortable: true, slotStatus: true },
          { prop: 'onlineFlag', label: '设备状态', align: 'center', sortable: true, slotStatus: true },
          { prop: 'userName', label: '操作人', align: 'center', sortable: true }
        ]
      },
      tableData: [
        // { name: 'ddd', code: 'sss', order: 1, status: 1 }
      ],
      dialogAgreementEdit: false,
      actionType: 'add',
      shopArr: [],
      shop: '',
      selectShop: ''
    }
  },
  computed: {
    ...mapGetters(['token', 'role'])
  },
  created () {
    this.getShops()
  },
  activated () {
    this.handleList(this.selectShop)
  },
  methods: {
    // 获取店铺
    getShops () {
      this.$store.dispatch('manage/StoreListMy', '').then(res => {
        if (res.code == '200') {
          var data = res.data
          this.shopArr = data
          if (this.role[0] == 'ADMIN') {
            this.shopArr = [{storeName: '全部', id: ''}].concat(this.shopArr)
          }
          this.shop = this.shopArr[0].id
        }
      })
    },
    // 新增
    handleAdd () {
      this.actionType = 'add'
      var item = {
        value: '/home/device-edit'
      }
      this.$store.commit('common/CLOSE_TAG', item)
      this.$nextTick(() => {
        this.$router.push({path: '/home/device-edit', query: {action: 'add'}})
      })
    },
    handleEdit (row) {
      this.actionType = 'edit'
      var item = {
        value: '/home/device-edit'
      }
      this.$store.commit('common/CLOSE_TAG', item)
      sessionStorage.setItem('device', JSON.stringify(row))
      this.$nextTick(() => {
        this.$router.push({path: '/home/device-edit', query: {action: 'edit', id: row.id}})
      })
    },
    handleView (row) {
      this.actionType = 'view'
      var item = {
        value: '/home/agreement-edit'
      }
      this.$store.commit('common/CLOSE_TAG', item)
      this.$nextTick(() => {
        this.$router.push({path: '/home/agreement-edit', query: {action: 'view', id: row.id}})
      })
    },
    // 改变pagesize
    handleSizeChange (val) {
      this.page.pageSize = val
      this.handleList(this.selectShop)
    },
    // 改变页码
    handleCurrentChange (val) {
      this.tablePage = val
      this.handleList(this.selectShop)
    },
    // 表格选择
    handleSelectChange (val) {
      // console.log(val)
      var arr = []
      for (var i = 0; i < val.length; i++) {
        arr.push(val[i].id)
      }
      this.selectIds = arr
      // console.log(this.selectIds)
    },
    // 列表接口
    handleList (storeId) {
      this.tableLoading = true
      var params = {
        storeId: storeId
      }
      this.$store.dispatch('manage/DeviceList', params).then(res => {
        if (res.code == '200') {
          var data = res.data
          this.tableData = data
          // this.page.total = parseInt(res.data.total)
          // this.page.currentPage = this.tablePage
          this.tableLoading = false
        }
        this.tableLoading = false
      }).catch(err => {
        console.log(err)
        this.tableLoading = false
      })
    },
    // 关闭弹窗
    closeDialog (params) {
      if (params == 'edit') {
        this.dialogAgreementEdit = false
      }
    },
    // 确认
    confirmDialog (params) {
      if (params == 'edit') {
        this.dialogAgreementEdit = false
        this.tablePage = 1
        this.handleList(this.selectShop)
      }
    },
    handleSearch () {
      this.selectShop = this.shop
      this.tablePage = 1
      this.handleList(this.selectShop)
    }
  }
}
</script>

<style lang="scss">
.device {
  padding: 0px !important;
  height: auto !important;
  .btn-list {
    float: right;
    margin-right: 10px;
    font-size: 12px;
    color: #409EFF;
    cursor: pointer;
  }
  .el-card__header {
    border-bottom: none;
  }
  .el-card__header,
  .el-card__body {
    padding: 15px;
  }
  .fix-lh {
    line-height: 32px;
  }
  .el-input.is-disabled .el-input__inner {
    color: #606266;
  }
  .left{
    min-height:500px;
    background:#fff;
  }
  .left-tree{
    width: 100%;
    min-height:600px;
    overflow: auto;
  }
  .left-top{
    width: 100%;
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 16px;
    color: #333;
    background: #e2e2e2;
    font-size: 15px;
  }
}
</style>
